<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class CustomModel extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'customModule:create {name}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '创建模块，自动生成模块目录(包括Controllers、Models、Contracts、Providers、Repositories、Exceptions)';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * 目录
     * @var array
     */
    protected $directory = [
        'Controllers',
        'Models',
        'Contracts',
        'Providers',
        'Repositories',
        'Exceptions'
    ];
    protected $routeFileName = 'routes.php';
    protected $providersFileName = 'AppServiceProvider.php';
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $name = $this->argument('name');
        $path = __DIR__ . '/../../' . $name;
        //创建模块目录
        if (is_dir($path)) {
            echo $name . ' directory already exists.' . PHP_EOL;
        } else {
            mkdir($path);
            echo $name . ' directory created successfully.' . PHP_EOL;
        }
        //创建模块内的文件夹
        foreach ($this->directory as $item) {
            //创建目录
            $folder = $path . '/' . $item;
            if (is_dir($folder)) {
                echo $item . ' directory already exists.' . PHP_EOL;
            } else {
                mkdir($path . '/' . $item);
                echo $item . ' directory created successfully.'.PHP_EOL;
            }
        }
        //创建路由文件
        $route = $path . '/' . $this->routeFileName;
        if (file_exists($route)) {
            echo $this->routeFileName . ' file already exists.' . PHP_EOL;
        } else {
            file_put_contents($route,'<?php'.PHP_EOL.' '.PHP_EOL);
            echo $this->routeFileName . ' file created successfully.' . PHP_EOL;
        }
        //创建路由文件
        $providers = $path . '/Providers/' . $this->providersFileName;
        if (file_exists($providers)) {
            echo $this->providersFileNamee . ' file already exists.' . PHP_EOL;
        } else {
            $baseProviderContent=file_get_contents(base_path("app/Base/Providers/AppServiceProvider.php"));
            $returnProvider=str_replace("\Base\\","\\".$name."\\",$baseProviderContent);
            file_put_contents($providers,$returnProvider);
            //修改配置文件
//            $config=require base_path("config/app.php");
//            $config2=["providers"=>"App\\".$name."\Providers\AppServiceProvider"];
//            $returnConfig=array_merge_recursive($config,$config2);
//            file_put_contents(base_path("config/app.php"),'<?php'.PHP_EOL.'return '.var_export($returnConfig,true).'; '.PHP_EOL);
           echo $this->providersFileName . ' file created successfully.' . PHP_EOL;
        }


    }
}
